package com.xyw.code.gateway.common.entity.form;

import com.alibaba.fastjson.JSONObject;
import com.xyw.code.gateway.common.entity.po.GatewayRoute;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xuyiwei
 * Date: 2020/2/5
 * Time: 下午7:40
 * Email: 1328312923@qq.com
 * Description: No Description
 **/
@EqualsAndHashCode
@ApiModel
@Data
@Slf4j
public class GatewayRouteForm  {
    @NotEmpty(message = "网关断言不能为空")
    @ApiModelProperty(value = "网关断言")
    private List<PredicateDefinition> predicates = new ArrayList<>();

    @ApiModelProperty(value = "网关过滤器信息")
    private List<FilterDefinition> filters = new ArrayList<>();

    @NotBlank(message = "uri不能为空")
    @ApiModelProperty(value = "网关uri")
    private String uri;

    @NotBlank(message = "路由id不能为空")
    @ApiModelProperty(value = "网关路由id")
    private String routeId;

    @ApiModelProperty(value = "排序")
    private Integer orders = 0;

    @ApiModelProperty(value = "网关路由描述信息")
    private String description;

    public GatewayRoute toPo(Class<GatewayRoute> clazz) {
        GatewayRoute gatewayRoute = BeanUtils.instantiateClass(clazz);
        BeanUtils.copyProperties(this,gatewayRoute);
        gatewayRoute.setFilters(JSONObject.toJSONString(this.getFilters()));
        gatewayRoute.setPredicates(JSONObject.toJSONString(this.getPredicates()));
        return gatewayRoute;
    }
}
